'use strict';

const Controller = require('egg').Controller;

class NoticeController extends Controller {

  async create() {
    const { ctx } = this;
    const body = ctx.request.body;

    const [ id ] = await ctx.service.notice.newNotice({
      userId: ctx.getUserId(),
      title: body.title,
      content: body.content,
    });

    ctx.body = {
      success: true,
      id,
    }
  }

  async getNoticeDetail() {
    const { ctx } = this;

    const [ noticeDetail ] = await ctx.service.notice.getNoticeDetail({
      noticeId: ctx.getQuery('noticeId'),
    })

    ctx.body = {
      success: true,
      noticeDetail,
    };
  }

  async getNoticeList() {
    const { ctx } = this;
    
    const listQuery = {};
    const [ option ] = ctx.getPageListOption();

    const [ list, totalCount ] = await ctx.service.notice.getNoticeList(listQuery, option);

    ctx.apiPageList(list, totalCount);
  }
}

module.exports = NoticeController;